<?php
require_once('classes/ticket.php');

class Transavia{
	private $carrier = "Transavia";
	private $user ="goedkope";
	private $userKey = "dFeankEn6";
	private $baseUrl = "http://transavia.nonstopwebservices.de/v1/GetAvailabilityAndPricing";
	private $search;
	private $url;
	private $retour;
	private $tickets = array();
	
	public function Transavia($searchObj){
		$this->search = $searchObj;
		$this->retour = $this->search->getRetour();
		$this->url = $this->buildUrl();
	}
	
	public function getTickets($index){
		$xml = simplexml_load_file($this->url);

		if(($xml->flights->flight->return != "") && ($this->search->getRetour() == true) || ($this->search->getRetour() == false) && ($xml->flights->flight->return == "")){
			//Transavia returns single flight even if flighttype = re 
			if(isset($xml->flights->flight )){
				foreach($xml->flights->flight as $flight){
					//DAGEN TOEVOEGEN
					$departureTime = new DateTime($flight->outward->fromTime);
					$arrivalTime = new DateTime($flight->outward->toTime);
					$interval = $departureTime->diff($arrivalTime);
					$interval = $interval->format('%H:%i');
		
					$ticket = new Ticket($index,
								     (string)$flight->deeplink,
								     (double)$flight->prices->TotalPrice,
								     (double)$flight->prices->TotalPrice['TotalPriceOnePassenger'],
								     (string)$this->carrier,
								     (int)$flight->prices->TotalPrice['TaxSurchargeOnePassenger'] * (int)$flight->prices->TotalPrice['PassengerAmount'],
								     0,
								     $interval);
					
				
					
					foreach($flight->outward as $outward){
						$ticket->setFlightOutbound((string)$outward['fromIATA'],
												   (string)$outward['toIATA'],
												   $departureTime,
												   $arrivalTime,
												   "Transavia",
												   $interval,
												   (string)$outward->FlightNumber);
					}
					if($this->search->getRetour()){
						$departureTime = new DateTime($flight->return->fromTime);
						$arrivalTime = new DateTime($flight->return->toTime);
						$interval = $departureTime->diff($arrivalTime);
						$interval = $interval->format('%H:%i');
					
						foreach($flight->return as $inward){

								$ticket->setFlightInbound((string)$inward['fromIATA'],
												   (string)$inward['toIATA'],
												   $departureTime,
												   $arrivalTime,
												   "Transavia",
												   $interval,
												   (string)$inward->FlightNumber);
						}
					}
				
					$this->tickets[$index] = $ticket;
					
					$index = $index +1;
				}
			}
		}
		return $this->tickets;
	}

	private function buildUrl(){
		$userAndPass = "?user=".$this->user."&key=".$this->userKey;
		$deptAirport = "&from_iata=".$this->search->getDeparturyCityIATA();							
		$arrAirport = "&to_iata=".$this->search->getDestinationCityIATA();
		$deptDate =  "&from_date=".$this->search->getDepartureDate();
		$passengers = $this->search->getAdults() + $this->search->getChildren() + $this->search->getInfants();
		$passengers = "&passengers=".$passengers;
		
		//Minimum URL
		//$this->baseUrl = $this->baseUrl.$userAndPass.$deptAirport.$arrAirport.$deptDate.$passengers;
		
		
		
		if($this->retour){
			$toDate = "&to_date=".$this->search->getReturnDate();
			$flightType ="&flighttype=re";
			$this->baseUrl = $this->baseUrl.$userAndPass.$deptAirport.$arrAirport.$deptDate.$toDate.$passengers.$flightType;
		}else{

			$this->baseUrl = $this->baseUrl.$userAndPass.$deptAirport.$arrAirport.$deptDate.$passengers;
		}
		
		
		//Transavia counts the person

		
		return $this->baseUrl;
	}
}